package com.asion.gankapp.view.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.asion.gankapp.R;
import com.asion.gankapp.applacation.MyApp;
import com.asion.gankapp.entrys.BaseEntry;
import com.asion.gankapp.utils.ThemeChangeUtil;

import org.greenrobot.greendao.query.Query;

import gl.com.app.Gank;
import gl.com.app.GankDao;

public class WebActivity extends AppCompatActivity implements SlidingPaneLayout.PanelSlideListener {

    private SlidingPaneLayout mWebSliding;
    private ImageButton mWebBack;
    private TextView mWebDesc;
    private WebView mWebWebview;
    private TextView mWebCollect;
    private Query<Gank> query;
    private BaseEntry.ResultsBean resultBean;
    private Gank gank1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeChangeUtil.changeTheme(this);
        setContentView(R.layout.activity_web);
        initView();

        Intent intent = getIntent();
        String desc = intent.getStringExtra("desc");
        String url = intent.getStringExtra("url");


        Bundle bundle = intent.getBundleExtra("bundle");
        if(bundle != null){
            resultBean = (BaseEntry.ResultsBean) bundle.getSerializable("resultBean");
            gank1 = (Gank) bundle.getSerializable("gank");
        }
        if(resultBean != null){
            query = MyApp.getGankDao().queryBuilder().where(GankDao.Properties.Desc.eq(resultBean.getDesc())).build();
        }

        if (gank1!= null && gank1.getCheked()) {
            mWebCollect.setText("已收藏");
        }

        if(query != null && query.list().size()!= 0 &&query.list().get(0).getCheked()){
            mWebCollect.setText("已收藏");
        }

        if(url == null&&desc == null){
            mWebCollect.setOnClickListener(v -> {
                if ("收藏".equals(mWebCollect.getText())) {
                    Gank gank = new Gank(null, resultBean.getDesc(), resultBean.getType(), resultBean.getUrl(), resultBean.getPublishedAt(), resultBean.getWho(), true);
                    if(query.list().size() == 0){
                        MyApp.getGankDao().insert(gank);
                    }
                    mWebCollect.setText("已收藏");
                }
            });
        }else{
            mWebCollect.setVisibility(View.GONE);
        }

        mWebSliding.setPanelSlideListener(this);
        mWebSliding.setSliderFadeColor(0X00000000);
        if (desc != null) {
            mWebDesc.setText(desc);
        } else if (resultBean != null) {
            mWebDesc.setText(resultBean.getDesc());
        } else {
            mWebDesc.setText(gank1.getDesc());
        }

        mWebWebview.getSettings().setTextZoom(100);
        mWebWebview.getSettings().setUseWideViewPort(true);
        mWebWebview.getSettings().setLoadWithOverviewMode(true);
        if (url != null) {
            mWebWebview.loadUrl(url);
        } else if (resultBean != null) {
            mWebWebview.loadUrl(resultBean.getUrl());
        } else {
            mWebWebview.loadUrl(gank1.getUrl());
        }
    }

    private void initView() {
        mWebSliding = (SlidingPaneLayout) findViewById(R.id.web_sliding);
        mWebBack = (ImageButton) findViewById(R.id.web_back);
        mWebDesc = (TextView) findViewById(R.id.web_desc);
        mWebWebview = (WebView) findViewById(R.id.web_webview);
        mWebBack.setOnClickListener(v -> finish());
        mWebCollect = (TextView) findViewById(R.id.web_collect);
    }

    @Override
    public void onPanelSlide(View panel, float slideOffset) {
    }

    @Override
    public void onPanelOpened(View panel) {
        this.finish();
    }

    @Override
    public void onPanelClosed(View panel) {

    }
}
